package com.carmaintain.servlets.merchant;

import java.io.IOException;
import java.util.Map;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.carmaintain.common.Constants;
import com.carmaintain.common.Utils;
import com.carmaintain.config.Configs;
import com.carmaintain.db.MerchantLib;
import com.carmaintain.db.UserLib;
import com.carmaintain.models.ResultObject;

/**
 * Servlet implementation class GetMaintainHistory
 */
public class GetMaintainHistory extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetMaintainHistory() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print("请使用POST方式调用");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ResultObject result = new ResultObject();
		Map<String, Object> maintainHistories;
		String merchantId = "";
		String carLicense = "";
		String nameOrMobile = "";
		String status = "";
		String startDate = "";
		String endDate = "";
		int page = 1;
		int pageSize = Configs.DEFAULT_PAGE_SIZE;
		
		try {
			request.setCharacterEncoding("UTF-8");
			if (!Utils.checkSign(request)) {
				result.setCode(Constants.CODE_SIGN_ERROR);
				result.setMessage(Constants.MESSAGE_WRONG_SIGN);
				response.setCharacterEncoding("UTF-8");
				response.getWriter().print(Utils.genJsonString(result));
				return;
			}
			merchantId = UserLib.checkUserLogin(request.getParameter("merchant_id"), request.getParameter("usercode"), Constants.USER_TYPE_MERCHANT);
			if (merchantId == null) {
				throw new Exception();
			}
			if (Utils.empty(request.getParameter("merchant_id")) && Utils.empty(request.getParameter("car_license"))) {
				result.setCode(Constants.CODE_MISS_PARAMATER);
				result.setMessage(Constants.MESSAGE_MISS_PARAMATER);
				response.setCharacterEncoding("UTF-8");
				response.getWriter().print(Utils.genJsonString(result));
				return;
			}
			if (!Utils.empty(request.getParameter("car_license"))) {
				carLicense = request.getParameter("car_license");
			}
			if (!Utils.empty(request.getParameter("name_or_mobile"))) {
				nameOrMobile = request.getParameter("name_or_mobile");
			}
			if (!Utils.empty(request.getParameter("status"))) {
				status = request.getParameter("status");
			}
			
			Pattern pattern = Pattern.compile("([0-9]{4})-([0-9]{2})-([0-9]{2})");
			
			if (!Utils.empty(request.getParameter("start_date")) && pattern.matcher(request.getParameter("start_date")).matches()) {
				startDate = request.getParameter("start_date");
			}
			if (!Utils.empty(request.getParameter("end_date")) && pattern.matcher(request.getParameter("end_date")).matches()) {
				endDate = request.getParameter("end_date");
			}
			if (!Utils.empty(request.getParameter("page"))) {
				try {
					page = Integer.parseInt(request.getParameter("page"));
					if (page < 1) {
						page = 1;
					}
				} catch (Exception e) {
					page = 1;
				}
			}
			if (!Utils.empty(request.getParameter("page_size"))) {
				try {
					pageSize = Integer.parseInt(request.getParameter("page_size"));
					if (pageSize < 1) {
						pageSize = Configs.DEFAULT_PAGE_SIZE;
					}
				} catch (Exception e) {
					pageSize = Configs.DEFAULT_PAGE_SIZE;
				}
			}
		} catch (Exception e) {
			result.setCode(Constants.CODE_WRONG_PARAMATER);
			result.setMessage(Constants.MESSAGE_WRONG_PARAMATER);
			Utils.addTestMessage(result, e);
			response.setCharacterEncoding("UTF-8");
			response.getWriter().print(Utils.genJsonString(result));
			return;
		}
		
		try {
			maintainHistories = MerchantLib.getMaintainHistory(merchantId, carLicense, nameOrMobile, status, startDate, endDate, page, pageSize);
			if (maintainHistories == null) {
				result.setCode(Constants.CODE_FAIL);
				result.setMessage(Constants.MESSAGE_FAIL);
				result.setResultData(maintainHistories);
			} else {
				result.setCode(Constants.CODE_SUCCESS);
				result.setMessage(Constants.MESSAGE_SUCCESS);
				result.setResultData(maintainHistories);
			}
			
		} catch (Exception e) {
			result.setCode(Constants.CODE_EXCEPTION);
			result.setMessage(Constants.MESSAGE_EXCETION);
			Utils.addTestMessage(result, e);
		}
		
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print(Utils.genJsonString(result));
	}

}
